﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
using System.IO; 

namespace HPCloud.Objects.Domain.Compute.Servers
{
    public class Log : BaseEntity
    {
        private string _content;

//=========================================================================================
/// <summary>
/// 
/// </summary>
//=========================================================================================
        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }
//=========================================================================================
/// <summary>
/// 
/// </summary>
/// <returns></returns>
//=========================================================================================
        private RSAParameters CreateParameters(string privateKeyPath)
        {
            StreamReader sr                 = new StreamReader(privateKeyPath);
            PemReader pr                    = new PemReader(sr);
            AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject();

            return DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private);
        }
//=========================================================================================
/// <summary>
/// Pulls the encrypted portion of the admin pw from the log.
/// </summary>
/// <returns></returns>
//=========================================================================================
        private string ExtractEncryptedPassword()
        {
            int startPos = this.Content.LastIndexOf("-----BEGIN BASE64-ENCODED ENCRYPTED PASSWORD-----") + 49;
            int endPos   = this.Content.IndexOf("-----END BASE64-ENCODED ENCRYPTED PASSWORD-----");

            return this.Content.Substring(startPos, endPos - startPos).Replace("\\n", "\n");
        }
//=========================================================================================
/// <summary>
/// 
/// </summary>
/// <param name="privateKeyPath"></param>
/// <returns></returns>
//=========================================================================================
        public string ExtractAdminPassword(string privateKeyPath)
        { 
            string encryptedPassword = this.ExtractEncryptedPassword();
            byte[] b64               = System.Convert.FromBase64String(encryptedPassword);
            RSAParameters rsaParams  = this.CreateParameters(privateKeyPath);
           
            // Import the Private key into the Provider..

            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
            RSAalg.ImportParameters(rsaParams);
            
            // Convert the decrypted admin pw back to string and return it..

            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            return enc.GetString(RSAalg.Decrypt(b64, false)).Replace("\r\n", String.Empty);           
        }
    }
}
